$(function(){
	// var code = GetQueryString("code")||"";
	var code = localStorage.exam_code || "";
	window.exam_code = code;
	var nowPage = 1, pageSize = 10;
	function getCalledStudentFun(){
		var prama = {
			page: nowPage,
			size: pageSize,
			code: code,
			success: function(res){
				if(res.code === 0){
					setStudentList(res, nowPage)
				}else{
					alert(res.msg)
				}
			}
		}
		getCalledStudent(prama);
	}
	getCalledStudentFun();
	function setStudentList(res,page){
		var results = res.data.results;
		var count = res.data.count;
		var previous = res.data.previous;
		var next = res.data.next;
		var page_count = 10;
		setPagination({
			previous: previous,
			next: next,
			count: count,
			page_count: page_count,
			nowPage: nowPage,
			continueFun: function(){
				$(".student-tr").remove();
				$(results).each(function(index, item){
					var clone = $(".clone-stutr").clone().removeClass("clone-stutr hide").addClass("student-tr");
					clone.attr({"data-id":item.code, "data-name": item.name, "data-sex": item.sex, "data-batch": item.batch, "data-status": item.status, "data-seat": item.seat, "data-examnumber": item.exam_number, "data-paperstype": item.papers_type, "data-papersnumber": item.papers_number, "data-exam": item.exam});
					clone.find(".stu-name").text(item.name);
					clone.find(".stu-number").text(item.exam_number);
					clone.find(".stu-sex").text(item.sex?"女":"男");
					clone.find(".stu-status").text(getStuStatus(item.status));
					$("table.exam-table").append(clone);
				})
				$(".hasnot-table").addClass("hide");
				$(".has-table").removeClass("hide");	
			}
		})		
	}
	// 获取用户信息
	getUserInfo({
		success: function(res){
			if (res.code == 0) {
				var data = res.data;
				$(".user-name").text(data.username);
			}else{
				window.location.href = "signin.html";
				// alert(res.msg);
			}
		},
		fail: function(res){
			var res = JSON.parse(res.responseText);
			window.location.href = "signin.html";
			// alert(res.msg)
		}
	})
	// 获取考试信息
	getExamInfo({
		code: code,
		success: function(res){
			if(res.code === 0){
				var data = res.data;
				$(".exam-name").text(data.name);
				$(".exam-status").text("（"+getExamStatus(data.status)+"）");
				$(".begin-time").text(data.begin_time);
				$(".end-time").text(data.end_time);
			}else{
				alert(res.msg);
			}
		},
		fail: function(res){
			var res = JSON.parse(res.responseText)
			alert(res.msg)
		}
	})
	// 点击确认考试
	function sendExamEntrance(option){
		var prama = {
			exam: option.exam,
			candidate: option.candidate
		}
		if(option.chose_all){
			prama.chose_all = option.chose_all
		}
		$.ajax({
			url: "/api/line_up/entrance/",
			type: "POST",
			dataType: "JSON",
			data: prama,
			success: function(res){
				option.success(res)
			},
			error: function(res){
				option.fail(res)
			}
		})
	}


	$(document).on("click",".sure-entrance", function(e){
		e.stopPropagation();
		var id = $(this).parents(".student-tr").attr("data-id");
		sendExamEntrance({
			exam: code,
			candidate: id,
			success: function(res){
				getCalledStudentFun();
				alert("确认入场成功");
			},
			fail: function(res){
				var res = JSON.parse(res.responseText)
            	alert(res.msg)
			}
		})
	})
	// 上一页
	$(document).on("click", "#previous_page:not(.disabled)", function(e){
		nowPage--;
		getStudentListFun();
	})
	// 下一页
	$(document).on("click", "#next_page:not(.disabled)", function(e){
		nowPage++;
		getStudentListFun();
	})
	// 选择某一页
	$(document).on("click", ".page-li:not(.disabled):not(.active)", function(e){
		var that = $(this);
		nowPage = parseFloat(that.text().trim()) || 1;
		getStudentListFun();
	})
})